package yartiss.service.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import yartiss.engine.simulation.Event;
import yartiss.engine.simulation.IEventAddedListener;
import yartiss.engine.simulation.Simulation;
import yartiss.engine.simulation.system.ITask;

public class DefaultEventsModel extends AbstractEventsModel {

	private Simulation simulation;
	private final ArrayList<Event> eventQue = new ArrayList<Event>();
	private final HashMap<ITask, List<Event>> eventsQueByTask = new HashMap<ITask, List<Event>>();
	private final HashMap<Integer, List<Event>> eventsQueByProc = new HashMap<Integer, List<Event>>();
	
	/**
	 * @return the simulation
	 */
	public Simulation getSimulation() {
		return simulation;
	}

	/**
	 * @param simulation the simulation to set
	 */
	public void setSimulation(Simulation sim) {
		if(sim != simulation){
			eventsQueByTask.clear();
			eventQue.clear();
			unregisterSimulationListeners();
			this.simulation = sim;
			registerSimulationListerners();
			fireEventAdded(null);
		}
	}
	
	/**
	 * 
	 * @param sim
	 */
	public DefaultEventsModel(Simulation sim){
		setSimulation(sim);
		
	}
	
	private final IEventAddedListener eventAddedListener = new IEventAddedListener() {
		@Override
		public void perfomredAction(Event e) {
			eventQue.add(e);
			List<Event> eq = null;
			if(!eventsQueByTask.containsKey(e.getTask())){
				eq = new ArrayList<Event>();
				eventsQueByTask.put(e.getTask(), eq);
			}
			else eq = eventsQueByTask.get(e.getTask());
			eq.add(e);
			
			if(e.getProcId()>0) {
				eq = null;
				if(!eventsQueByProc.containsKey(Integer.valueOf(e.getProcId()))){
					eq = new ArrayList<Event>();
					eventsQueByProc.put(Integer.valueOf(e.getProcId()), eq);
				}
				else eq = eventsQueByProc.get(Integer.valueOf(e.getProcId()));
				eq.add(e);
			}
			fireEventAdded(e);
		}
	};
	
	/**
	 * 
	 */
	private void registerSimulationListerners() {
		if(simulation != null)
			simulation.addEventAddedListerner(eventAddedListener);
	}
	
	private void unregisterSimulationListeners() {
		if(simulation != null)
			simulation.removeEventAddListener(eventAddedListener);
		
	}
	
	@Override
	public Iterable<ITask> getAllTasks() {
		return getSimulation().getSimulationParameters().getTaskSet();
	}

	/**
	 * 
	 */
	@Override
	public List<Event> getEvents(long startTime, long duration) {
		int from=-1, to=0;
		long  endTime = startTime+duration;
		for (Event e : eventQue) {
			if(from<0  && e.getDate()==startTime)from=eventQue.indexOf(e);
			if(from>=0 && e.getDate()==endTime)to=eventQue.indexOf(e);
			if(from>=0 && e.getDate()>endTime)break;
		}
		if(from<0)return new LinkedList<Event>();
		return eventQue.subList(from, to);
	}

	/**
	 * 
	 */
	@Override
	public List<Event> getTaskEvents(ITask task, long startTime, long duration) {
		List<Event> taskEvents = eventsQueByTask.get(task);
		int from=-1, to=0;
		long  endTime = startTime+duration;
		for (Event e : taskEvents) {
			if(from<0  && e.getDate()==startTime)from=taskEvents.indexOf(e);
			if(from>=0 && e.getDate()==endTime)to=taskEvents.indexOf(e);
			if(from>=0 && e.getDate()>endTime)break;
		}
		return taskEvents.subList(from,  to);
	}

	@Override
	public List<Event> getAllEvents() {
		return new LinkedList<Event>(eventQue);
	}

	@Override
	public List<Event> getAllTaskEvents(ITask task) {
		if(eventsQueByTask.containsKey(task))
			return new LinkedList<Event>(eventsQueByTask.get(task));
		return new ArrayList<Event>();
	}

	@Override
	public long getFinishTime() {
		if(eventQue.size()>0)
			return eventQue.get(eventQue.size()-1).getDate();
		return 0;
	}

	@Override
	public List<Event> getProcEvents(int procId, long startTime, long duration) {
		List<Event> taskEvents = eventsQueByProc.get(Integer.valueOf(procId));
		int from=-1, to=0;
		long  endTime = startTime+duration;
		for (Event e : taskEvents) {
			if(from<0  && e.getDate()==startTime)from=taskEvents.indexOf(e);
			if(from>=0 && e.getDate()==endTime)to=taskEvents.indexOf(e);
			if(from>=0 && e.getDate()>endTime)break;
		}
		return taskEvents.subList(from,  to);
	}

	@Override
	public List<Event> getAllProcEvents(int procId) {
		if(eventsQueByProc.containsKey(Integer.valueOf(procId)))
			return new LinkedList<Event>(eventsQueByProc.get(Integer.valueOf(procId)));
		return new ArrayList<Event>();
	}

	@Override
	public Iterable<Integer> getAllProcsIds() {
		return eventsQueByProc.keySet();
	}

	

	
}
